﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using RT = Coolzon.Metadata.Runtime;

namespace Coolzon.UI.Core.Converters
{
    /// <summary>
    /// <TextAlign Value="Left"/>
    /// </summary>
    public class TextAlignmentConverter : ValueConverter
    {
        public object Convert(ConvertContext context, RT.Property property)
        {
            string strAlign = string.IsNullOrEmpty(property.ValueString) == false ? property.ValueString : property.ValueNode.GetAtrributeValue("Value");

            TextAlignment alignment = TextAlignment.Left;
            Enum.TryParse<TextAlignment>(strAlign, out alignment);
            return alignment;
        }

        public RT.ValueNode ConvertBack(ConvertContext context, object value, object dtTagData)
        {
            RT.ValueNode valueNode = new RT.ValueNode("TextAlign");
            valueNode.Attributes = new Dictionary<string, string>();
            if ((TextAlignment)value == TextAlignment.Left)
            {
                valueNode.Attributes.Add("Value", "LEFT");
            }
            else if ((TextAlignment)value == TextAlignment.Right)
            {
                valueNode.Attributes.Add("Value", "RIGHT");
            }
            else if ((TextAlignment)value == TextAlignment.Center)
            {
                valueNode.Attributes.Add("Value", "CENTER");
            }
            else
            {
                valueNode.Attributes.Add("Value", "JUSTIFY");
            }
            return valueNode;
        }
    }
}
